package com.weitu.train.plan.service.impl;

import com.weitu.train.plan.entity.Train;
import com.weitu.train.plan.mapper.TrainMapper;
import com.weitu.train.plan.service.TrainService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainMapper trainMapper;

    @Override
    public Train selectById(String id) {
        return this.trainMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        return this.trainMapper.deleteById(id);
    }

    @Override
    public boolean updateByIdSelective(Map record) {
        boolean pflag=false;
        boolean cflag=insertData(record);
        int pboo=this.trainMapper.updateByIdSelective(record);
        if (pboo>0){
            pflag=true;
        }
        return cflag&&pflag;
    }

    @Override
    @Transactional
    public boolean insertSelective(Map record) {
        boolean pflag=false;
        boolean cflag=insertData(record);
        int pboo= this.trainMapper.insertSelective(record);
        if (pboo>0){
            pflag=true;
        }
        return cflag&&pflag;
    }

    @Override
    @Transactional
    public boolean insertData(Map<String, Object> paramMap) {
        String planId=paramMap.get("id").toString();
        boolean cState=false;
        if (paramMap.get("certificateTypeId")!=null){
            List<Map> list=new ArrayList<Map>();
            if (paramMap.get("certificateTypeId")instanceof String){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("planId",planId);
                map.put("certificateTypeId",paramMap.get("certificateTypeId").toString());
                map.put("delState",0);
                list.add(map);
            }else {
                List<String> conIds=(List<String>)paramMap.get("certificateTypeId");
                if(conIds!=null&& !"".equals(conIds)){
                    if (conIds.size()>0){
                        for (String conid:conIds){
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("planId",planId);
                            map.put("certificateTypeId",conid);
                            map.put("delState",0);
                            list.add(map);
                        }
                    }
                }
            }
            int dboo=this.trainMapper.deleteCertificateTypeIds(planId);
            int iboo=this.trainMapper.insertCertificateTypeIds(list);
            if (iboo>0){
                cState=true;
            }
        }else {
            cState=true;
        }
        return cState;
    }

    @Override
    public int insertCertificateTypeIds(List<Map> maps) {
        return 0;
    }

    @Override
    public int deleteCertificateTypeIds(@Param("id") String id) {
        return 0;
    }

    @Override
    public List<Train> selectAllPlans(Map map) {
        return this.trainMapper.selectAllPlans(map);
    }

    @Override
    public int selectAllPlansCount(Map map) {
        return this.trainMapper.selectAllPlansCount(map);
    }

    @Override
    public List<Train> selectListPlans(Map map) {
        return this.trainMapper.selectListPlans(map);
    }


    @Override
    public int selectAllListPlansCount(Map map) {
        return this.trainMapper.selectAllListPlansCount(map);
    }

    @Override
    public List<Map<String, Object>> selectAnalysis(Map<String, Object> map) {
        return this.trainMapper.selectAnalysis(map);
    }

    @Override
    public List<Map<String, Object>> selectAnalysisPerson(Map<String, Object> map) {
        return this.trainMapper.selectAnalysisPerson(map);
    }
}